/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.ricemine.services.uidocument;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kuali.rice.kim.bo.entity.KimEntityAddress;
import org.kuali.rice.kim.bo.entity.KimEntityAffiliation;
import org.kuali.rice.kim.bo.entity.KimEntityEmail;
import org.kuali.rice.kim.bo.entity.KimEntityEmploymentInformation;
import org.kuali.rice.kim.bo.entity.KimEntityName;
import org.kuali.rice.kim.bo.entity.KimEntityPhone;
import org.kuali.rice.kim.bo.entity.dto.KimEntityEntityTypeInfo;
import org.kuali.rice.kim.bo.entity.dto.KimEntityInfo;
import org.kuali.rice.kim.bo.entity.dto.KimPrincipalInfo;
import org.kuali.rice.kim.bo.entity.impl.KimEntityAffiliationImpl;
import org.kuali.rice.kim.bo.entity.impl.KimEntityEmploymentInformationImpl;
import org.kuali.rice.kim.bo.entity.impl.KimEntityNameImpl;
import org.kuali.rice.kim.bo.entity.impl.KimEntityPrivacyPreferencesImpl;
import org.kuali.rice.kim.bo.ui.PersonDocumentAddress;
import org.kuali.rice.kim.bo.ui.PersonDocumentAffiliation;
import org.kuali.rice.kim.bo.ui.PersonDocumentEmail;
import org.kuali.rice.kim.bo.ui.PersonDocumentEmploymentInfo;
import org.kuali.rice.kim.bo.ui.PersonDocumentName;
import org.kuali.rice.kim.bo.ui.PersonDocumentPhone;
import org.kuali.rice.kim.bo.ui.PersonDocumentPrivacy;
import org.kuali.rice.kim.document.IdentityManagementPersonDocument;
import org.kuali.rice.kim.service.IdentityService;
import org.kuali.rice.kim.service.impl.UiDocumentServiceImpl;
import org.kuali.rice.kim.util.KimConstants;
import org.kuali.rice.kns.util.TypedArrayList;

/**
 *
 * @author tcarroll
 */
public class LdapUiDocumentServiceImpl extends UiDocumentServiceImpl {

  private final Log log = LogFactory.getLog(getClass());

  @Override
  public void loadEntityToPersonDoc(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId) {
    IdentityService identityService = getIdentityService();
    KimPrincipalInfo kpi = identityService.getPrincipal(principalId);
    identityManagementPersonDocument.setPrincipalId(kpi.getPrincipalId());
    identityManagementPersonDocument.setPrincipalName(kpi.getPrincipalName());
//NOT AVAILABLE IN INFO OBJECT    identityManagementPersonDocument.setPassword(kpi.getPassword());
    identityManagementPersonDocument.setActive(kpi.isActive());
    KimEntityInfo kei = identityService.getEntityInfo(kpi.getEntityId());
    identityManagementPersonDocument.setEntityId(kei.getEntityId());
    identityManagementPersonDocument.setActive(kei.isActive());
////    identityManagementPersonDocument.setAdHocRoutePersons();
////    identityManagementPersonDocument.setAdHocRouteWorkgroups();
    identityManagementPersonDocument.setAffiliations(loadAffiliationsToo(kei.getAffiliations(), kei.getEmploymentInformation()));
////    identityManagementPersonDocument.setAutoIncrementSet();
////    identityManagementPersonDocument.setBoNotes();
//    identityManagementPersonDocument.setCampusCode();
//    identityManagementPersonDocument.setCitizenships(kei.getCitizenships());
////    identityManagementPersonDocument.setDelegationMembers();
////    identityManagementPersonDocument.setDelegations();
////    identityManagementPersonDocument.setDocumentHeader();
////    identityManagementPersonDocument.setDocumentNumber();
////    identityManagementPersonDocument.setExtension();
//    identityManagementPersonDocument.setExternalIdentifiers();
////    identityManagementPersonDocument.setGroups();
////    identityManagementPersonDocument.setIfRolesEditable();
    identityManagementPersonDocument.setNames(loadNamesToo(identityManagementPersonDocument, principalId, kei.getNames(), kei.getPrivacyPreferences().isSuppressName()));
////    identityManagementPersonDocument.setNewCollectionRecord();
////    identityManagementPersonDocument.setObjectId();
////    identityManagementPersonDocument.setPassword();
////    identityManagementPersonDocument.setPessimisticLocks();
//    identityManagementPersonDocument.setPrivacy(kei.getPrivacyPreferences());
////    identityManagementPersonDocument.setRoles();
////    identityManagementPersonDocument.setTaxId();
//    identityManagementPersonDocument.setUnivId();
////    identityManagementPersonDocument.setVersionNumber();
    KimEntityEntityTypeInfo keeti = null;
    for (KimEntityEntityTypeInfo t : kei.getEntityTypes()) {
      if (KimConstants.EntityTypes.PERSON.equals(t.getEntityTypeCode())) {
        keeti = t;
      }
    }
    if (keeti != null) {
      identityManagementPersonDocument.setEmails(loadEmailsToo(identityManagementPersonDocument, principalId, keeti.getEmailAddresses(), kei.getPrivacyPreferences().isSuppressName()));
      identityManagementPersonDocument.setPhones(loadPhonesToo(identityManagementPersonDocument, principalId, keeti.getPhoneNumbers(), kei.getPrivacyPreferences().isSuppressName()));
      identityManagementPersonDocument.setAddrs(loadAddressesToo(identityManagementPersonDocument, principalId, keeti.getAddresses(), kei.getPrivacyPreferences().isSuppressName()));
    }
//////////		if ( ObjectUtils.isNotNull( kimEntity.getPrivacyPreferences() ) ) {
//////////			identityManagementPersonDocument.setPrivacy(loadPrivacyReferences(kimEntity.getPrivacyPreferences()));
//////////		}
//////////    for (KimEntityExternalIdentifierImpl extId : kimEntity.getExternalIdentifiers()) {
//////////      if (KimConstants.PersonExternalIdentifierTypes.TAX.equals(extId.getExternalIdentifierTypeCode())) {
//////////////????      if (extId.getExternalIdentifierTypeCode().equals(TAX)) {
//////////        identityManagementPersonDocument.setTaxId(extId.getExternalId());
//////////      }
//////////    }
//////////    List<? extends Group> groups = getGroupService().getGroupsForPrincipal(identityManagementPersonDocument.getPrincipalId());
//////////    loadGroupToPersonDoc(identityManagementPersonDocument, groups);
//////////    loadRoleToPersonDoc(identityManagementPersonDocument);
//////////    loadDelegationsToPersonDoc(identityManagementPersonDocument);
  }

  @Override
  protected List<PersonDocumentAddress> loadAddresses(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId, List<KimEntityAddress> entityAddresses, boolean suppressDisplay) {
    throw new UnsupportedOperationException("use loadAddressesToo since extend interface was not referenced in generics");
  }

  protected List<PersonDocumentAddress> loadAddressesToo(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId, List<? extends KimEntityAddress> entityAddresses, boolean suppressDisplay) {
    List<PersonDocumentAddress> pdas = new TypedArrayList(PersonDocumentAddress.class);
    if (entityAddresses != null) {
      for (KimEntityAddress a : entityAddresses) {
        if (a.isActive()) {
          PersonDocumentAddress pda = new PersonDocumentAddress();
          //UI should take care of privacy setting
          pda.setActive(a.isActive());
//          pda.setActiveFromDate();
//          pda.setActiveToDate();
//ONLY AVAILABLE IN RDBMS IMPL NOT IN INFO OBJECTS.  NEED TO DERIVE THESE FROM TYPECODES					pda.setAddressType(a.getAddressType());
          pda.setAddressTypeCode(a.getAddressTypeCode());
//          pda.setAutoIncrementSet();
//          pda.setBoNotes();
          pda.setCityName(a.getCityNameUnmasked());
          pda.setCountryCode(a.getCountryCodeUnmasked());
          pda.setDflt(a.isDefault());
//          pda.setDocumentNumber();
          pda.setEdit(true);
          pda.setEntityAddressId(a.getEntityAddressId());
          pda.setEntityTypeCode(a.getEntityTypeCode());
//          pda.setExtension();
          pda.setLine1(a.getLine1Unmasked());
          pda.setLine2(a.getLine2Unmasked());
          pda.setLine3(a.getLine3Unmasked());
//          pda.setNewCollectionRecord();
//          pda.setObjectId();
//          pda.setPersonDocument();
          pda.setPostalCode(a.getPostalCodeUnmasked());
          pda.setStateCode(a.getStateCodeUnmasked());
//          pda.setVersionNumber();
          pdas.add(pda);
        }
      }
    }
    return pdas;
  }

  @Override
  protected List<PersonDocumentAffiliation> loadAffiliations(List<KimEntityAffiliationImpl> affiliations, List<KimEntityEmploymentInformationImpl> empInfos) {
    throw new UnsupportedOperationException("use loadAffilitationsToo since extend interface was not referenced in generics");
  }

  protected List<PersonDocumentAffiliation> loadAffiliationsToo(List<? extends KimEntityAffiliation> affiliations, List<? extends KimEntityEmploymentInformation> empInfos) {
//    List<PersonDocumentAffiliation> pdas = new ArrayList<PersonDocumentAffiliation>();
    List<PersonDocumentAffiliation> pdas = new TypedArrayList(PersonDocumentAffiliation.class);
    if (affiliations != null) {
      for (KimEntityAffiliation a : affiliations) {
        if (a.isActive()) {
          PersonDocumentAffiliation pda = new PersonDocumentAffiliation();
          pda.setActive(a.isActive());
//        pda.setActiveFromDate();
//        pda.setActiveToDate();
//        pda.setAffiliationType();
          pda.setAffiliationTypeCode(a.getAffiliationTypeCode());
//        pda.setAutoIncrementSet();
//        pda.setBoNotes();
          pda.setCampusCode(a.getCampusCode());
          pda.setDflt(a.isDefault());
//        pda.setDocumentNumber();
          pda.setEdit(true);
          pda.setEntityAffiliationId(a.getEntityAffiliationId());
//        pda.setExtension();
//        pda.setNewCollectionRecord();
//        pda.setNewEmpInfo();
//        pda.setObjectId();
//        pda.setPersonDocument();
//        pda.setVersionNumber();
          pda.refreshReferenceObject("affiliationType");
          pdas.add(pda);
          if (log.isDebugEnabled()) {
            log.debug("loaded affiliation: affiliationTypeCode=" + pda.getAffiliationTypeCode());
          }
          // employment informations
// EntityAffiliationImpl does not define empinfos as collection
//        List<PersonDocumentEmploymentInfo> pdei = new ArrayList<PersonDocumentEmploymentInfo>();
          List<PersonDocumentEmploymentInfo> pdeis = new TypedArrayList(PersonDocumentEmploymentInfo.class);
          if (empInfos != null) {
            for (KimEntityEmploymentInformation e : empInfos) {
              if (e.isActive() && pda.getEntityAffiliationId().equals(e.getEntityAffiliationId())) {
//							if (empInfo.isActive() && StringUtils.equals(pda.getEntityAffiliationId(), empInfo.getEntityAffiliationId())) {
                PersonDocumentEmploymentInfo pdei = new PersonDocumentEmploymentInfo();
                pdei.setActive(e.isActive());
//          pdei.setActiveFromDate();
//          pdei.setActiveToDate();
//          pdei.setAffiliation();
//          pdei.setAutoIncrementSet();
                pdei.setBaseSalaryAmount(e.getBaseSalaryAmount());
//          pdei.setBoNotes();
//          pdei.setDocumentNumber();
                pdei.setEdit(true);
                pdei.setEmployeeId(e.getEmployeeId());
                pdei.setEmployeeStatusCode(e.getEmployeeStatusCode());
                pdei.setEmployeeTypeCode(e.getEmployeeTypeCode());
                pdei.setEmploymentRecordId(e.getEmploymentRecordId());
//          pdei.setEmploymentStatus();
//          pdei.setEmploymentType();
                pdei.setEntityAffiliationId(e.getEntityAffiliationId());
                pdei.setEntityEmploymentId(e.getEntityEmploymentId());
//          pdei.setExtension();
//          pdei.setNewCollectionRecord();
//          pdei.setObjectId();
                pdei.setPrimary(e.isPrimary());
                pdei.setPrimaryDepartmentCode(e.getPrimaryDepartmentCode());
//ONLY ONE FROM ORIGINAL METHOD THAT I DON'T HAVE A VALUE FOR SO FAR          pdei.setVersionNumber(); //??0L
                pdei.refreshReferenceObject("employmentType");
                pdeis.add(pdei);
              }
              pda.setEmpInfos(pdeis);
            }
          }
        }
      }
    }
    return pdas;
  }

  @Override
  protected List<PersonDocumentEmail> loadEmails(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId, List<KimEntityEmail> entityEmails, boolean suppressDisplay) {
    throw new UnsupportedOperationException("use loadEmailsToo since extend interface was not referenced in generics");
  }

  protected List<PersonDocumentEmail> loadEmailsToo(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId, List<? extends KimEntityEmail> entityEmails, boolean suppressDisplay) {
    List<PersonDocumentEmail> pdes = new TypedArrayList(PersonDocumentEmail.class);
    if (entityEmails != null) {
      for (KimEntityEmail e : entityEmails) {
        if (e.isActive()) {
          PersonDocumentEmail pde = new PersonDocumentEmail();
          //UI should take care of privacy setting
          pde.setActive(e.isActive());
//          pde.setActiveFromDate();
//          pde.setActiveToDate();
//          pde.setAutoIncrementSet();
//          pde.setBoNotes();
          pde.setDflt(e.isDefault());
//          pde.setDocumentNumber();
          pde.setEdit(true);
          pde.setEmailAddress(e.getEmailAddressUnmasked());
//ONLY AVAILABLE IN RDBMS IMPL NOT IN INFO OBJECTS.  NEED TO DERIVE THESE FROM TYPECODES          pde.setEmailType(e.getEmailType());
          pde.setEmailTypeCode(e.getEmailTypeCode());
          pde.setEntityEmailId(e.getEntityEmailId());
          pde.setEntityTypeCode(e.getEntityTypeCode());
//          pde.setExtension();
//          pde.setNewCollectionRecord();
//          pde.setObjectId();
//          pde.setPersonDocument();
//          pde.setVersionNumber();
          pdes.add(pde);
        }
      }
    }
    return pdes;
  }

  @Override
  protected List<PersonDocumentName> loadNames(IdentityManagementPersonDocument personDoc, String principalId, List<KimEntityNameImpl> names, boolean suppressDisplay) {
    throw new UnsupportedOperationException("use loadNamesToo since extend interface was not referenced in generics");
  }

  protected List<PersonDocumentName> loadNamesToo(IdentityManagementPersonDocument personDoc, String principalId, List<? extends KimEntityName> names, boolean suppressDisplay) {
    List<PersonDocumentName> pdns = new TypedArrayList(PersonDocumentName.class);
    if (names != null) {
      for (KimEntityName n : names) {
        if (n.isActive()) {
          PersonDocumentName pdn = new PersonDocumentName();
          //UI should take care of privacy setting
          pdn.setActive(n.isActive());
//          pdn.setActiveFromDate();
//          pdn.setActiveToDate();
//          pdn.setAutoIncrementSet();
//          pdn.setBoNotes();
          pdn.setDflt(n.isDefault());
//          pdn.setDocumentNumber();
          pdn.setEdit(true);
//          pdn.setEntityId();
          pdn.setEntityNameId(n.getEntityNameId());
//ONLY AVAILABLE IN RDBMS IMPL NOT IN INFO OBJECTS.  NEED TO DERIVE THESE FROM TYPECODES          pdn.setEntityNameType(n.getEntityNameType());
//          pdn.setExtension();
          pdn.setFirstName(n.getFirstNameUnmasked());
          pdn.setLastName(n.getLastNameUnmasked());
          pdn.setMiddleName(n.getMiddleNameUnmasked());
          pdn.setNameTypeCode(n.getNameTypeCode());
//          pdn.setNewCollectionRecord();
//          pdn.setObjectId();
//          pdn.setPersonDocument();
          pdn.setSuffix(n.getSuffixUnmasked());
          pdn.setTitle(n.getTitleUnmasked());
//          pdn.setVersionNumber();
          pdns.add(pdn);
        }
      }
    }
    return pdns;
  }

  @Override
  protected List<PersonDocumentPhone> loadPhones(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId, List<KimEntityPhone> entityPhones, boolean suppressDisplay) {
    throw new UnsupportedOperationException("use loadPhonesToo since extend interface was not referenced in generics");
  }

  protected List<PersonDocumentPhone> loadPhonesToo(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId, List<? extends KimEntityPhone> entityPhones, boolean suppressDisplay) {
    List<PersonDocumentPhone> pdps = new TypedArrayList(PersonDocumentPhone.class);
    if (entityPhones!=null) {
      for (KimEntityPhone p : entityPhones) {
        if (p.isActive()) {
          PersonDocumentPhone pdp = new PersonDocumentPhone();
          //UI should take care of privacy setting
          pdp.setActive(p.isActive());
//          pdp.setActiveFromDate();
//          pdp.setActiveToDate();
//          pdp.setAutoIncrementSet();
//          pdp.setBoNotes();
          pdp.setCountryCode(p.getCountryCodeUnmasked());
          pdp.setDflt(p.isDefault());
//          pdp.setDocumentNumber();
          pdp.setEdit(true);
          pdp.setEntityPhoneId(p.getEntityPhoneId());
          pdp.setEntityTypeCode(p.getEntityTypeCode());
//          pdp.setExtension();
          pdp.setExtensionNumber(p.getExtensionNumberUnmasked());
//          pdp.setNewCollectionRecord();
//          pdp.setObjectId();
//          pdp.setPersonDocument();
          pdp.setPhoneNumber(p.getPhoneNumberUnmasked());
//ONLY AVAILABLE IN RDBMS IMPL NOT IN INFO OBJECTS.  NEED TO DERIVE THESE FROM TYPECODES                    pdp.setPhoneType(p.getPhoneType());
          pdp.setPhoneTypeCode(p.getPhoneTypeCode());
//          pdp.setVersionNumber();
          pdps.add(pdp);
        }
      }
    }
    return pdps;
  }

  @Override
  protected PersonDocumentPrivacy loadPrivacyReferences(KimEntityPrivacyPreferencesImpl privacyPreferences) {
    return super.loadPrivacyReferences(privacyPreferences);
  }
}
